//
//  HD_HSP_MineMessage_ViewController.m
//  HDLiaoNingMeseum
//
//  Created by HDHaoShaoPeng on 2017/8/7.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HD_HSP_MineMessage_ViewController.h"

@interface HD_HSP_MineMessage_ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *myID;
@property (weak, nonatomic) IBOutlet UILabel *myNickName;
@property (weak, nonatomic) IBOutlet UILabel *nowMode;
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;

@property(strong,nonatomic)NSMutableArray *friendsArray;
@end

static HDDeclare *declare;
@implementation HD_HSP_MineMessage_ViewController
{
    NSString *_inputNum;
}

-(NSMutableArray *)friendsArray
{
    if (!_friendsArray) {
        _friendsArray = [NSMutableArray arrayWithCapacity:50];
    }
    return _friendsArray;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    declare = [HDDeclare sharedDeclare];
    
    self.titleLabel.text = declare.mineInfo;
    self.titleLabel.font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:18.0];
    self.myID.text = [NSString stringWithFormat:@"ID:%@",declare.userID];
    self.myNickName.text = [NSString stringWithFormat:@"%@%@",declare.nameLabel,declare.nickName];
    
    UIFont *font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:16.0];
    self.myID.font = font;
    self.myNickName.font = font;
    self.nowMode.font = [UIFont fontWithName:@"FZQKBYSJW--GB1-0" size:18.0];
    self.nowMode.numberOfLines = 2;
    self.myID.textColor = Text_Color;
    self.myNickName.textColor = Text_Color;
    self.nowMode.textColor = Text_Color;
    
    
    [self getMyFriends];
}

-(void)getMyFriends
{
    NSString *requestUrl = [NSString stringWithFormat:@"%@%@",[HDDeclare IP_Request_Header],@"index.php?g=mapi&m=Friend&a=get_group_member"];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:declare.userID,@"device_id",nil];
    [HD_LY_NetHelper requestWithType:POST withUrl:requestUrl withParams:params toShowVC:self success:^(id response) {
        NSDictionary *dicData = response;
        if ([dicData[@"status"] intValue] == 1)
        {
            NSArray *tempArr = [dicData objectForKey:@"data"];
            [self.friendsArray removeAllObjects];
            for (NSDictionary *tempDic in tempArr) {
                HD_HSP_Mine_Model *model = [[HD_HSP_Mine_Model alloc]init];
                model.nick_name = [tempDic objectForKey:@"nick_name"];
                model.name = [tempDic objectForKey:@"name"];
                model.group_id = [tempDic objectForKey:@"group_id"];
                [self.friendsArray addObject:model];
            }
            
            [self.myTableView reloadData];
            
        }else {
            
        }
    } fail:^(NSError *error) {
        
    } showHUD:NO isCache:NO];
}


-(void)uploadNickName
{
    NSString *requestUrl = [NSString stringWithFormat:@"%@%@",[HDDeclare IP_Request_Header],@"index.php?g=mapi&m=User&a=modify_nick_name"];
    NSDictionary *params = [NSDictionary dictionaryWithObjectsAndKeys:declare.userID,@"device_id",@"2",@"app_kind",_inputNum,@"name", nil];
    [HD_LY_NetHelper requestWithType:POST withUrl:requestUrl withParams:params toShowVC:self success:^(id response) {
        NSDictionary *dicData = response;
        if ([dicData[@"status"] intValue] == 1)
        {
            self.myNickName.text = [NSString stringWithFormat:@"%@%@",declare.nameLabel,_inputNum];
        }else {
            
        }
    } fail:^(NSError *error) {
        
    } showHUD:NO isCache:NO];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -------- TableView Delegate --------

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60.0f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 50.0f;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.friendsArray count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return declare.myFriend;
}

- (UITableViewCell*) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"mineCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell= [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    HD_HSP_Mine_Model *myfriend = [self.friendsArray objectAtIndex:indexPath.row];
    cell.textLabel.text = myfriend.nick_name;
    cell.detailTextLabel.text = myfriend.name;
    cell.backgroundColor = [UIColor clearColor];
    UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"cell_selected"]];
    cell.selectedBackgroundView = image;
    cell.imageView.image = [UIImage imageNamed:@"mine_friend_icon"];
    
    UIView *view = [cell viewWithTag:201];
    [view setHidden:YES];
    
    cell.textLabel.textColor = [UIColor blackColor];
    cell.detailTextLabel.textColor = [UIColor blackColor];
    NSString *strName = [NSString stringWithFormat:@"ios%@", declare.userID];
    if ([cell.detailTextLabel.text isEqualToString:strName])
    {
        cell.textLabel.textColor = [UIColor redColor];
        cell.detailTextLabel.textColor = [UIColor redColor];
    }
    
    return cell;
}

#pragma mark - 点击方法
- (IBAction)editeNickName:(id)sender {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:declare.joinNumberNeed message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:declare.alertCancel style:UIAlertActionStyleCancel handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:declare.alertOK style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        UITextField *login = alert.textFields.firstObject;
        _inputNum = login.text;
        [self uploadNickName];
    }];
    [alert addAction:cancelAction];
    [alert addAction:okAction];
    
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField){
        //        textField.placeholder = @"登录";
    }];
    alert.textFields.firstObject.keyboardType = UIKeyboardTypeNumberPad;
    [self presentViewController:alert animated:YES completion:nil];
}

- (IBAction)backClicked:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}


@end
